import numpy as np
import plotly.graph_objects as go
# Erzeuge x-y-Gitter
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
# Berechne Abstand vom Ursprung für jeden Punkt
R = np.sqrt(X**2 + Y**2)
# Berechne Funktionswerte
Z = X**2 + Y**2
# Maske für kreisförmiges Gebiet mit Radius 5 erstellen
mask = R > 5
Z = np.ma.masked_array(Z, mask)
# Erstelle Contourplot mit Plotly
fig = go.Figure()
# Füge Konturlinien hinzu
fig.add_trace(
go.Contour(
x=x,
y=y,
z=Z,
contours=dict(
start=0,
end=25,
size=2.5, # Entspricht 11 Levels zwischen 0 und 25
showlabels=True
),
colorscale='viridis',
colorbar=dict(title='f(x,y) = x² + y²')
)
)
# Kreislinie für den Rand der Definitionsmenge hinzufügen
theta = np.linspace(0, 2*np.pi, 100)
x_circle = 5 * np.cos(theta)
y_circle = 5 * np.sin(theta)
fig.add_trace(
go.Scatter(
x=x_circle,
y=y_circle,
mode='lines',
line=dict(color='red', width=2),
name='Definitionsmenge (Radius 5)'
)
)
# Layout anpassen
fig.update_layout(
title='Höhenlinien der Funktion f(x,y) = x² + y² mit kreisförmiger Definitionsmenge',
xaxis_title='x-Achse',
yaxis_title='y-Achse',
width=800,
height=600,
xaxis=dict(
scaleanchor="y",
scaleratio=1,
showgrid=True
),
yaxis=dict(
showgrid=True
)
)
# Zeige den Plot an
fig.show()
Z
masked_array(
data=[[--, --, --, ..., --, --, --],
[--, --, --, ..., --, --, --],
[--, --, --, ..., --, --, --],
...,
[--, --, --, ..., --, --, --],
[--, --, --, ..., --, --, --],
[--, --, --, ..., --, --, --]],
mask=[[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True],
...,
[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True]],
fill_value=1e+20)
import numpy as np
import plotly.graph_objects as go
# Erzeuge ein regelmäßiges Gitter
grid_size = 100
x = np.linspace(-5, 5, grid_size)
y = np.linspace(-5, 5, grid_size)
X, Y = np.meshgrid(x, y)
# Berechne Abstand vom Ursprung für jeden Punkt
R = np.sqrt(X**2 + Y**2)
# Berechne Funktionswerte und setze Werte außerhalb des Kreises auf None
Z = X**2 + Y**2
Z = np.where(R <= 5, Z, None) # Setze Werte außerhalb des Kreises auf None
# Erstelle Contourplot mit Plotly
fig = go.Figure()
# Füge Konturlinien hinzu
fig.add_trace(
go.Contour(
x=x,
y=y,
z=Z,
contours=dict(
start=0,
end=25,
size=2.5, # Entspricht 11 Levels zwischen 0 und 25
showlabels=True
),
colorscale='viridis',
colorbar=dict(title='f(x,y) = x² + y²'),
hoverinfo='x+y+z', # Zeige x, y und z-Werte beim Hovern
line=dict(width=0.5, color='black') # Dünne schwarze Linien für Konturen
)
)
# Kreislinie für den Rand der Definitionsmenge hinzufügen
theta = np.linspace(0, 2*np.pi, 100)
x_circle = 5 * np.cos(theta)
y_circle = 5 * np.sin(theta)
fig.add_trace(
go.Scatter(
x=x_circle,
y=y_circle,
mode='lines',
line=dict(color='red', width=2),
name='Definitionsmenge (Radius 5)'
)
)
# Layout anpassen
fig.update_layout(
title='Höhenlinien der Funktion f(x,y) = x² + y² mit kreisförmiger Definitionsmenge',
xaxis_title='x-Achse',
yaxis_title='y-Achse',
width=800,
height=600,
xaxis=dict(
scaleanchor="y",
scaleratio=1,
showgrid=True
),
yaxis=dict(
showgrid=True
),
# Entferne Hintergrundfarbe für Punkte außerhalb des Kreises
plot_bgcolor='white'
)
# Zeige den Plot an
fig.show()